home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 352_01.zip / STRDEMO.CPP < prev    next >
C/C++ Source or Header  |  1993-04-10  |  6KB  |  190 lines

  1. // STRDEMO.CXX  - demo file for C++ String class.
  2. //
  3. //    member functions are provided to do almost all common string manipulations
  4. //        including:     assignment, comparison, concatenation,
  5. //                    finding substrings, finding any of a series of letters
  6. //                    extracting substrings, removing from middle
  7. //                    tokenize string in components
  8. //                    trimming whitespace in several different ways
  9. //                            (ie: from end only, from whole string, 
  10. //                                 duplicate whitespace only, etc...)
  11. //                    translating upper/lower case or arbitrary translation.
  12. //                    find and replace operations.
  13. // 
  14. //    Case sensitivity can be turned on or off for any of the functions.
  15. //    The sizes of the string buffers are taken care of automatically.
  16. //
  17. //
  18. //    To compile and link, construct a project file 
  19. //        that includes     WTS.LIB  ( windows lib for wmalloc() function ).
  20. //                        STRPPS.LIB ( lib with a all string plus plus routines )
  21. //                        strdemo.cpp (this module) 
  22. //
  23. //    D Blum 8/90
  24.  
  25. #include <stdlib.h>
  26. #include <iostream.h>
  27.  
  28. #include "wtwgpp.h"
  29.  
  30. #define ON     1
  31. #define OFF 0
  32.             
  33. #pragma argsused            // allows us to ignore argv without a warning 
  34.  
  35.  
  36. int main ( int argc, char **argv)
  37.     {
  38.     winit ('T');        // for memory allocation routines.
  39.     if ( argc > 1 )
  40.         {
  41.         cout<< "String demo run with caseSens flag turned ON\n";
  42.         String::caseSens = ON;
  43.         }
  44.     else
  45.         {
  46.         cout<<     "String demo run with caseSens flag turned OFF\n"
  47.             <<    "To run with caseSens turned ON, supply any parameter\n"; 
  48.         // OFF is default.    
  49.         }
  50.         
  51.     // constructing and comparing strings.
  52.     // NOTE differences between upper and lower case.    
  53.     // NOTE sometimes referring to Strings and sometimes to char *
  54.     String apples("apples");
  55.     String Apples("Apples");
  56.     String oranges("oranges");
  57.     String Oranges("Oranges");
  58.  
  59.     if ( apples < Oranges )     cout<<"apples are less than Oranges. ";    
  60.     else                        cout<<"Oranges are less than apples. ";
  61.  
  62.     if ( Apples == "apples" )     cout<<"Apples and apples are the same\n";
  63.     else                        cout<<"Apples and apples are not the same\n";
  64.  
  65.  
  66.     // CONCATENATE 3 Strings and assign results to another string.
  67.     // NOTE mixing Strings with traditional 'strings' which are just char*
  68.     //
  69.     String     MixedFruit = apples + " & " + oranges;
  70.             MixedFruit += "\n";
  71.     cout << " Concatenation: " << MixedFruit ;
  72.  
  73.  
  74.     // INSERTION 
  75.     String nowtime("Now time for all good men");
  76.     String forall("is the ");
  77.     nowtime.insert ( forall, 4 );    
  78.     cout << "\nString insertion: " << nowtime << "\n";
  79.     
  80.     // SUBSTRING EXTRACTION.
  81.     //        the source String nowtime is unchanged. a new String is created,
  82.     //        the address of the new String is returned by String::substring()
  83.     //        NOTE: the cout operator << can use either a String or a &String.
  84.     //                    so you can use ptrs to Strings or Strings themselves.
  85.     //        NOTE: you must explicitly delete the String created by substring()
  86.     //              or it will persist till the end of the program.
  87.     String *Sptr;
  88.     Sptr = nowtime.substring( 7,5 );          // creates a new String. 
  89.     cout << "Substring: (\"the t\") "<<  Sptr <<"\n";
  90.     delete Sptr;                            // IMPORTANT! you must delete
  91.  
  92.     // FIND CHARACTERS or STRINGS.
  93.     //        functions in the String class include find() findAny() findNot()
  94.     //         similar to ANSI C functions strchr(), strstr(), strpbrk(), etc...
  95.     //        but these are easier to use, and all of them obey String::caseSens
  96.     // 
  97.     String  fs = "Now is the time for all good men";
  98.     String  St = "TIME";
  99.  
  100.     int  st_pos = fs.find (St);        // could also use:    fs.find("TIME");
  101.     if ( st_pos == -1 )
  102.         {
  103.         cout<< "uppercase TIME not found in text: " << fs << "\n";
  104.         }
  105.     else
  106.         {
  107.         cout << "The position of TIME in: Now is the time... is(11) " 
  108.              <<  st_pos << "\n";
  109.         }
  110.  
  111.  
  112.     // find a vowel using findAny, which returns position of the vowel. 
  113.     //    and use the index operator [] to retrieve the vowel itself 
  114.     st_pos = fs.findAny("AEIOU");
  115.     cout      <<"The first vowel in \"Now is the time...\" is ";
  116.     if ( st_pos == -1 ) cout << "NO CAPITAL VOWELS FOUND";
  117.     else                cout << fs[st_pos];
  118.         
  119.  
  120.     // TOKENIZE
  121.     //         tokenize() allows you to break a string up into parts.
  122.     //        each part is placed in a new String, the original is broken down.
  123.     //        NOTE: 1) the separator chars are lost.
  124.     //              2) the search for separator chars obeys String::caseSens
  125.     //              3) the newly created Strings must be explicitly deleted.
  126.  
  127.     cout <<"\nTokenizing...NOTE + signs replace any tokens.\n";
  128.  
  129.     while ( fs != NULL )
  130.         {
  131.         Sptr = fs.tokenize ("AEIOU");        // tokenize() returns a &String
  132.         cout<< Sptr <<"+";                    //         (NOTE << passes &String
  133.         delete Sptr;                        //... which must be deleted    
  134.         }
  135.     cout << "\n";
  136.  
  137.  
  138.  
  139.     // TRIM() and SQUEEZE() remove unwanted chars from string
  140.     //    most useful for removing whitespace.
  141.     //  NOTE both squeeze and trim shorten the string.
  142.     //
  143.     fs = "Many small letters x, y and z at the end: xxxyzzzyyyx";
  144.     cout << fs;
  145.     cout << "\nXYZ removed from end of string: " << fs.trim("XYZ") << "\n";
  146.  
  147.     cout << "All XYZ removed wherever found: " << fs.squeeze("XYZ") << "\n";
  148.         
  149.  
  150.  
  151.  
  152.     // noExtra() can simplify the distribution of whitespace in a String.
  153.     //        noExtra(str) compresses all multiple occurence of chars from str
  154.     //                     and replaces the first occurence with the first char.
  155.     //
  156.     char *whitespace = " \r\n\t";        //NOTE space is first char provided.
  157.     fs = "String  \twith\nimproved \r     white\t\t\tspace";
  158.     cout << "\nOriginal whitespace: " << fs;
  159.     cout << "\nSimplify whitespace: " << fs.noExtra (whitespace) << "\n";
  160.  
  161.  
  162.         
  163.         
  164.     // TRANSLATE() exchanges chars from 1 translate table with another
  165.     //        There are functions toUpper() and toLower() as well.
  166.     //                    ie: String X="abcdef";  X.toUpper();
  167.     //         NOTE caseSens flag controls translation.
  168.     //
  169.     fs = "Old MacDonald Had A Farm";
  170.     cout<< "Original: " << fs << "\n";
  171.     cout<< "Encoded:  " 
  172.         << fs.translate ("abcdefghijklmnopqrstuvwxyz",
  173.                          "12345678901234567890123456" ) << "\n";        
  174.     
  175.     
  176.     
  177.     
  178.     // REPLACE() find all occurences of char *orig in String,
  179.     //            replace each occurence with char *replace.
  180.     //    NOTE    1) caseSens, also, 
  181.     //            2) lengths of orig and replace don't need to be equal.
  182.     //                (the String length is adjusted for you.)
  183.     //
  184.     fs = "A horse, a horse, my kingdom for a horse.";
  185.     cout << fs.replace ( "a horse", "two giraffes" ) << "\n";
  186.     // with apologies to W.S.    
  187.         
  188.     return 0;    // main()
  189.     }
  190. //------------------- end of STRDEMO.CPP --------------------------//